package org.jgs1904.service.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.jgs1904.bean.Pager;
import org.jgs1904.dao.CommentDao;
import org.jgs1904.dao.PostDao;
import org.jgs1904.dao.impl.CommentDaoImpl;
import org.jgs1904.dao.impl.PostDaoImpl;
import org.jgs1904.entity.Comment;
import org.jgs1904.entity.Post;
import org.jgs1904.service.PostService;

/**
 * 	帖子业务类
 * @author junki
 * @date 2020年3月26日
 */
public class PostServiceImpl implements PostService {

	private PostDao postDao = new PostDaoImpl();
	
	private CommentDao commentDao = new CommentDaoImpl();
	
	public List<Post> getList() throws SQLException {
		
		return postDao.selectList();
	}

	public Post add(Post post) throws SQLException {
		return postDao.insert(post);
	}

	public Post getOneById(Long id) throws SQLException {
		Post result = postDao.selectOneById(id);
		List<Comment> commentList = commentDao.selectListByPostId(id);
		result.setComment_list(commentList);
		result.setComment_count(commentList.size());
		return result;
	}

	@Override
	public Pager<Post> getPage(Long currentPage, Long pageSize, Long pageNumShowLength, Map<String, String[]> paramMap) throws SQLException {
		return postDao.selectPage(currentPage, pageSize, pageNumShowLength, paramMap);
	}

	@Override
	public void addAll(List<Post> list) throws SQLException {
		for (Post post : list) {
			postDao.insert(post);
		}
	}

}
